<template>
	<view class="imgCont">
		<view class="main">
			<!-- 分类 -->
			<view class="active_tit paddingNone">
				<view class="imgTit">
				<slot></slot>贷款用途影像资料
				</view> 
				<view class="imgBtn" @click="isShow">
					{{showText}}
				</view>
		
				
			</view>

		</view>
		<view v-show="show">
			<view class="main" v-for="item in type" >
				<view class="active_tit">
					<view class="tit2">
						{{item.name}}
					</view> 
				</view>
				<block v-for="zitem in imageData[item.code]"  >
					<view class="choose-img-box" v-if="zitem.base64!=''&&zitem.businessTag=='贷款用途'">
						<icon v-if="zitem.base64!=''" class="icon" type="cancel" size="26" @click="remove(imageData[item.code],zitem,item.code)" />
						<image class="img" :src="zitem.img"  @click="lookImg(zitem.img)" />
					</view>
					
				</block>
				<view class="choose-img-box" >
					<image class="img" src="/static/enterprise_card.png" @click="chooseImg(imageData[item.code],item.code)" />
				</view>
			</view>
		</view>
		
	</view>
	
</template>

<script>
	import chooseImgToBase64 from "@/utils/chooseImgToBase64";
	export default {
		data() {
			return {	
				showText:'展开',
				show:false,
				type:[
					{'name':'采购合同/凭证',code:'SDMS_0203_24'},
				],
				imageData:{
					SDMS_0203_24:[{img:'/static/enterprise_card.png',base64:''}],
				}
			}
		},
		props:{
			changeFlag: {
				type:Number
			},
		},
		watch:{
			changeFlag:{
				handler:function(res){
					//console.warn('数据变化',res)
					this.imageData = getApp().globalData.imageData.scarData
				},
				deep:true
			},
			imageData:{
				handler:function(res){
					//console.warn('数据变化',res)
					getApp().globalData.imageData.scarData = res
				},
				deep:true
			}
		},
		mounted() {
			this.fileQuery()
		},
		methods: {
			fileQuery(){
				
				let obj = {
					typeCode:'SDMS_0203',
					businessId:getApp().globalData.stdOffsurBaiscInfo.businessId,
				}
				this.sendRequest({
					url: 'ext/findImageList',//'offsur/fileQuery',
					method: 'post',
					hideLoading: true,
					data:  obj,
					complete: (res) => {
						let list = Object.keys(this.imageData)
						let imgData = []
						if (res.code == '200') {
							res.data.map((item,key)=>{
								item.img = 'data:image/png;base64,'+item.fileBase64
								item.base64 = item.fileBase64
							})
						
							//console.log(res.data)
							
							for(let i=0;i<res.data.length;i++){
								for(let j=0;j<list.length;j++){
									if(res.data[i].typeCode==list[j]){
										this.imageData[list[j]].push(res.data[i])
									}
								}
							}
							
						}
					}
				})
			},
			chooseImg(name,code) {
				console.log(name)
				chooseImgToBase64(
					(base64, base64SourceData, file) => {
						//console.log(base64SourceData)
						let imgData = {
							img : base64,
							base64 : base64SourceData
						}
						
						//this.idCardOcr("FRONT");
						this.fileUpload(code,file.name,base64SourceData,imgData,name)
						
					},2048
			
				);
			},
			fileUpload(code,fileName,base64,item,name){
				console.log(name)
				let obj = {
					fileBase64:base64,
					businessId:getApp().globalData.stdOffsurBaiscInfo.businessId,
					typeCode:code,
					fileName:fileName,
					currentUserId:getApp().globalData.stdOffsurBaiscInfo.customerId,
					businessTag:'贷款用途'
				}
				this.sendRequest({
					url: 'ext/upload',
					method: 'post',
					hideLoading: false,
					data:  obj,
					complete: (res) => {
						
						if (res.code == '200') {
							item.fileId = res.data.fileId
							item.businessTag = '贷款用途'
							name.push(item)
						}else{
							uni.showToast({
								icon:'none',
								title:"图片上传失败，请重试",
								duration:2000
							})
						}
					}
				})
			},
			
			remove(name,item){
				let obj = {
					businessId:getApp().globalData.stdOffsurBaiscInfo.businessId,
					fileId:item.fileId,
					currentUserId:getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/deleteImage',
					method: 'post',
					hideLoading: true,
					data:  obj,
					complete: (res) => {
						
						if (res.code == '200') {
							name.remove(item)
						}
					}
				})
				
			},
			isShow(){
				if(this.show){
					this.show = false
					this.showText = '展开'
				}else{
					this.show = true
					this.showText = '收起'
				}
			}
		}
	}
</script>

<style>

</style>
